<?php
/**
 * Created by zhang.
 * User: zhang
 * Date: 2018/7/25
 * Time: 22:07
 */

namespace app\index\controller;


use app\index\model\barInfo;
use app\index\model\comment;
use app\index\model\coupon;
use think\View;

class BarHall extends Base
{

    public function barList(){
        $where['bar_stat'] = 1;
        $barList = (new barInfo()) ->getBarList($where);
        $return =[];
        foreach ($barList as $item) {
            preg_match('/<img.*?src="(.*?)".*?>/is',$item['bar_info'],$matches);//http://可要可不要
            $temp['barImg'] = $matches[1];
            $temp['barName'] = $item['bar_name'];
            $temp['barId'] = $item['bar_id'];
            $return[] = $temp;
        }

        $this->view->barlist = $return;
        return $this->view ->fetch();
    }


    public function barAllInfo($id){
        $barId = intval($id);
        $where['bar_id'] = $barId;
        $barInfo = (new  barInfo()) ->getBarInfo($where);
        preg_match('/<img.*?src="(.*?)".*?>/is',$barInfo['bar_info'],$matches);//http://可要可不要
        $this->view->banerimg =$matches[1];
        $this ->view ->barInfo = $barInfo;
        $this ->view ->evaInfo = $this->getBarEva($barId);
        $this ->addBarClickCount($barId);
        $this ->view ->coupon = $this ->getCouponList($barId);
        return $this ->view ->fetch();
    }

    //获取评论
    private function getBarEva($id){
        $where['com_bar_id'] = $id;
        $evaInfo = (new comment()) ->getEva($where);
        return $evaInfo;
    }

    /**
     * 点击一次人气加1
     */
    private function addBarClickCount($id){
        $where['bar_id'] = $id;
        (new barInfo())->IncClickPeople($where);
    }

    //获取优惠券
    private function getCouponList($id){
        $where['coupon_bar_id'] = $id;
//        $where['coupon_use_begin_time'] = array('lt',date("y-m-d H:i:s"));
        $where['coupon_use_end_time'] = array('gt',date("y-m-d H:i:s"));
        $where['coupon_stat'] = 1;
        $where['coupon_surplus_num'] = array('gt',0);
        $result = (new coupon())->getCouponList($where);
//        var_dump($result);exit;
        return $result;
    }

}